package com.meals.system.api.system;

import com.meals.system.api.system.vo.WhitesMealGoodsDetailVO;
import com.meals.system.api.system.vo.WhitesMealSearchSellerVO;
import com.meals.system.common.Constants;
import com.meals.system.common.ServiceResultEnum;
import com.meals.system.common.SystemException;
import com.meals.system.config.annotation.TokenToSystemUser;
import com.meals.system.entity.SystemUser;
import com.meals.system.entity.SystemUserToken;
import com.meals.system.entity.WhitesMealGoods;
import com.meals.system.service.WhitesMealSystemGoodsService;
import com.meals.system.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/10
 */
@RestController
@Api(value = "v1", tags = "0-4.白餐系统页面商品相关接口")
@RequestMapping("/user-api/v1")
public class WhitesMealSystemGoodsAPI {

    private static final Logger logger = LoggerFactory.getLogger(WhitesMealSystemGoodsAPI.class);

    @Resource
    private WhitesMealSystemGoodsService whitesMealSystemGoodsService;

    @GetMapping("/goods/search")
    @ApiOperation(value = "商品搜索接口", notes = "根据关键字进行搜索")
    public Result<PageResult<List<WhitesMealSearchSellerVO>>> search(@RequestParam(required = false) @ApiParam(value = "搜索关键字") String keyword,
                                                                     @RequestParam(required = false) @ApiParam(value = "商家id") Long sellerId,
                                                                     @RequestParam(required = false) @ApiParam(value = "orderBy") String orderBy,
                                                                     @RequestParam(required = false) @ApiParam(value = "搜索关键字") Integer pageNumber,
                                                                     @TokenToSystemUser SystemUser loginSystemUser){

        logger.info("good search api，keyword={},sellerId={},orderBy={},pageNumber={},userId={}",keyword,sellerId,orderBy,pageNumber,loginSystemUser.getUserId());

        Map params = new HashMap(8);
        // 两个搜索参数: 商家id和关键字
        // 两个搜索参数都为空，直接返回异常
        if (sellerId == null && StringUtils.isEmpty(keyword)){
            SystemException.fail("搜索参数异常");
        }
        if (pageNumber == null || pageNumber < 1){
            pageNumber = 1;
        }
        params.put("sellerId", sellerId);
        params.put("page", pageNumber);
        params.put("limit", Constants.GOODS_SEARCH_PAGE_LIMIT);
        // 对keyword做过滤 去掉空格
        if (!StringUtils.isEmpty(keyword)){
            params.put("keyword", keyword);
        }
        if (!StringUtils.isEmpty(orderBy)){
            params.put("orderBy", orderBy);
        }
        // 搜索上架状态下的商品
        params.put("goodsSellStatus", Constants.SELL_STATUS_UP);
        // 封装商品数据
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(whitesMealSystemGoodsService.searchWhitesMealGoods(pageUtil));
    }

    @GetMapping("/goods/detail/{goodsId}")
    @ApiOperation(value = "商品详情接口",notes = "传参为商品id")
    public Result<WhitesMealGoodsDetailVO> goodsDetail(@ApiParam(value = "商品id") @PathVariable("goodsId") Long goodsId, @TokenToSystemUser SystemUser loginSystemUser){
        logger.info("goods detail api,goodsId={},userId={}", goodsId, loginSystemUser.getUserId());
        if (goodsId < 1){
            return ResultGenerator.genFailResult("参数异常");
        }
        WhitesMealGoods goods = whitesMealSystemGoodsService.getWhitesMealGoodsById(goodsId);
        if (goods == null){
            return ResultGenerator.genFailResult("参数异常");
        }
        // 判断商品是否上架了
        if (Constants.SELL_STATUS_UP != goods.getGoodsSellStatus()){
            SystemException.fail(ServiceResultEnum.GOODS_PUT_DOWN.getResult());
        }
        WhitesMealGoodsDetailVO goodsDetailVO = new WhitesMealGoodsDetailVO();
        BeanUtil.copyProperties(goods, goodsDetailVO);
        goodsDetailVO.setGoodsCarouselList(goods.getGoodsCarousel().split(","));
        return ResultGenerator.genSuccessResult(goodsDetailVO);
    }

}
